<?php
namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;
class Transfer extends Base {

    /**
     * 转账记录
     */
    public function initialize()
    {
        parent::initialize();
        $wallet = logic('Wallet')->getName();
        View::assign('wallet',$wallet);
        View::assign('dztype',logic('Wallet')->getTxType());
    }
    public function index(){
        //$map = $this->_search();
        if(IS_AJAX){
            $page = input('page');
            $limit = input('limit')?:15;
            $map = [];
            if(input('user_id')){
                $map['w.user_id'] = input('user_id');
            }
            if(input('to_user_id')){
                $map['w.to_user_id'] = input('to');
            }
            if(input('username')){
                $map['w.username'] = input('username');
            }
            if(input('to_username')){
                $map['w.to_username'] = input('to_username');
            }
            if(input('time_start') && input('time_end')){
                $s_time = strtotime(input('time_start'));
                $e_time = strtotime(input('time_end'));
                $map['w.w_time'] = ['between',[$s_time,$e_time]];
            }
            if(input('from')){
                $map['w.from'] = input('from');
            }
            if(input('to')){
                $map['w.to'] = input('to');
            }

            $count = Db::name('transfer_log')->alias('w')->where($map)->count();

            $field = 'w.*';
            $list = Db::name('transfer_log')
                ->alias('w')
                ->join('users u','w.user_id=u.user_id','left')
//                ->join('users u1','w.to_user_id=u.user_id','left')
                ->field($field)
                ->where($map)
                ->order('w.id desc ')
                ->page($page,$limit)
                ->select();
            $wallet = logic('Wallet')->getName();
            foreach($list as $k=>$v){
                $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
                $v['from_name'] = $wallet[$v['from']];
                $v['to_name'] = $wallet[$v['to']];
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$list]);
        }
        return View::fetch();
    }
    //同意提现
    public function agrees(){
        $ids=I('post.id');
        if(!$ids) {
            $this->error("请选择同意提现的记录");
        }
        $modtime=time();
        $map=array('id'=>array('in',$ids),'status'=>0);
        $list=Db::name('Withdrawals')->where($map)->field('id,user_id')->select();
        if(!$list){
            $this->error('状态有误');
        }
        Db::name('Withdrawals')->where($map)->save(array('status'=>1,'edittime'=>$modtime));

        $this->success('操作成功！');
    }
    //拒绝提现
    public function refunds(){
        $ids=I('post.id');
        if(!$ids) {
            $this->error("请选择拒绝提现的记录");
        }
        $modtime=time();

        $map = array('id'=>array('in',$ids),'status'=>0);
        $list=Db::name('Withdrawals')->where($map)->field('id,user_id,total,tx_type')->select();
        if(!$list){
            $this->error('状态有误');
        }
        Db::startTrans();
        foreach($list as $key=>$val) {
            $rs = Db::name('withdrawals')->where(array('id'=>$val['id']))->save(array('status'=>2,'edittime'=>$modtime));
            if(!$rs){
                Db::rollback();
                $this->error('更新状态失败');
            }
            //返钱
            $result=qianbao($val['user_id'],$val['total'],$val['tx_type'],'拒绝兑换','后台拒绝兑换返回');
            if(!$result['status']){
                Db::rollback();
                $this->error($result['msg']);
            }
        }
        Db::commit();
        $this->success('拒绝提现成功');
    }
    /**
     * 导出订单
     */
    public function excel()
    {
        $page = input('page');
        $limit = input('limit')?:15;
        $map = [];
        if(input('user_id')){
            $map['w.user_id'] = input('user_id');
        }
        if(input('to_user_id')){
            $map['w.to_user_id'] = input('to');
        }
        if(input('username')){
            $map['w.username'] = input('username');
        }
        if(input('to_username')){
            $map['w.to_username'] = input('to_username');
        }
        if(input('time_start') && input('time_end')){
            $s_time = strtotime(input('time_start'));
            $e_time = strtotime(input('time_end'));
            $map['w.w_time'] = ['between',[$s_time,$e_time]];
        }
        if(input('from')){
            $map['w.from'] = input('from');
        }
        if(input('to')){
            $map['w.to'] = input('to');
        }
        $field = 'w.*';
        $count = Db::name('transfer_log')
            ->alias('w')
            ->join('users u','w.user_id=u.user_id')
            ->field($field)
            ->where($map)
            ->count();
        $list = Db::name('transfer_log')
            ->alias('w')
            ->join('users u','w.user_id=u.user_id')
            ->field($field)
            ->where($map)
            ->page($page,$limit)
            ->order('w.id desc ')
            ->select();
        foreach($list as $k=>$v){
            $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            $list[$k] = $v;
        }
        $title = ['ID','会员ID','会员编号','转入会员ID','转入会员编号','转账金额','手续费','到账金额','转出钱包','转入钱包','操作时间'];
        $wallet = logic('Wallet')->getName();
        foreach($list as $k=>$v){
            $v['w_time'] = date('Y-m-d H:i:s',$v['w_time']);
            $list[$k] = $v;
            $arr = [
                $v['id'],$v['user_id'],$v['username'],$v['to_user_id'],$v['to_username'],$v['money'],$v['tax'],$v['to_money'],$wallet[$v['from']],$wallet[$v['to']],$v['w_time']
            ];
            $data[] = $arr;
        }
        return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'title'=>$title]);
        //dump($title);
        //dump($data);exit;
        $this->export_excel($title,$data);
    }

}
